package huawei;

import java.util.*;

/**
 * 手机APP防成谜系统
 */
public class AppUseTime {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();

        List<Used> usedList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] strings = scanner.nextLine().split(" ");
            Used used = new Used(strings[0], Integer.parseInt(strings[1]), Integer.parseInt(strings[2].replace(":", "")), Integer.parseInt(strings[3].replace(":", "")));
            usedList.add(used);
        }

        // 根据begin排序，便于计算是否被其他app抢断
        usedList.sort(Comparator.comparingInt(e -> e.begin));

        for (int i = 0; i < usedList.size(); i++) {
            Used pre = usedList.get(i);
            for (int j = i + 1; j < usedList.size(); j++) {
                Used next = usedList.get(j);

                if (pre.end >= next.begin) {
                    if (pre.priority > next.priority) {
                        next.begin = pre.end + 1;
                    } else if (pre.priority < next.priority) {
                        pre.end = next.begin - 1;
                    } else {
                        next.begin = pre.end + 1;
                    }
                }
            }
        }

        Integer time = Integer.parseInt(scanner.nextLine().replace(":", ""));


        Integer curIndex = -1; // 目前应该那个app在使用
        for (int i = 0; i < usedList.size(); i++) {
            Used used = usedList.get(i);
            if (used.begin <= time && used.end >= time) {
                // 在时间范围内
                curIndex = i;
                break;
            }
        }

        if (curIndex == -1) {
            System.out.println("NA");
        } else {
            System.out.println(usedList.get(curIndex).name);
        }
    }

    public static class Used{
        String name;
        Integer priority;
        Integer begin;
        Integer end;

        public Used(String name, Integer priority, Integer begin, Integer end) {
            this.name = name;
            this.priority = priority;
            this.begin = begin;
            this.end = end;
        }
    }

}
